package com.fw.ui.view.profile.merchant.event;

import static org.springframework.beans.factory.config.BeanDefinition.SCOPE_PROTOTYPE;

import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.io.FilenameUtils;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.fw.ui.bao.MessageBAO;
import com.fw.ui.util.ApplicationHelper;
import com.fw.ui.view.event.AbstractButtonClickListener;
import com.fw.ui.view.profile.EditMerchantProfileView;
import com.vaadin.server.FileResource;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.Upload.Receiver;
import com.vaadin.ui.Upload.SucceededEvent;
import com.vaadin.ui.Upload.SucceededListener;

@Component
@Scope(SCOPE_PROTOTYPE)
public class EditMerchantProfileUploadButtonClickListener extends AbstractButtonClickListener implements Receiver, SucceededListener {

    private static final long serialVersionUID = 1L;
    public File file;

    @Override
    public void uploadSucceeded(SucceededEvent event) {
	// Show the uploaded file in the image viewer
	try {
	    if (file != null) {
		Path path = Paths.get(file.getPath());
		byte[] logoImageByteArray = Files.readAllBytes(path);
		final EditMerchantProfileView editMerchantProfileView = (EditMerchantProfileView) this.getBasedComponent();
		this.previewImage(editMerchantProfileView);
		editMerchantProfileView.getMerchantProfileBean().setLogoImageByteArray(logoImageByteArray);
		editMerchantProfileView.getMerchantProfileBean().setContentType(event.getMIMEType());
		editMerchantProfileView.getMerchantProfileBean().setFileName(event.getFilename());
		editMerchantProfileView.getMerchantProfileBean().setFileExtension(FilenameUtils.getExtension(file.getPath()));

		event.getUpload().getWidth();
		event.getUpload().getHeight();
		event.getUpload().getUploadSize();
	    }
	} catch (Exception e) {
	    e.printStackTrace();
	}
    }

    private void previewImage(EditMerchantProfileView editMerchantProfileView) {
	editMerchantProfileView.getLayoutLogo().setWidthUndefined();
	editMerchantProfileView.getLayoutLogo().setHeightUndefined();
	editMerchantProfileView.getImgMerchantLogo().setVisible(true);
	editMerchantProfileView.getLblMerchantLogoHint().setVisible(false);
	editMerchantProfileView.getImgMerchantLogo().setSource(new FileResource(file));
    }

    private void showHint(EditMerchantProfileView editMerchantProfileView) {
	editMerchantProfileView.getImgMerchantLogo().setSource(null);
	editMerchantProfileView.getImgMerchantLogo().setVisible(false);
	editMerchantProfileView.getLayoutLogo().setWidth("300px");
	editMerchantProfileView.getLayoutLogo().setHeight("150px");
	editMerchantProfileView.getLblMerchantLogoHint().setVisible(true);

    }

    @Override
    public OutputStream receiveUpload(String filename, String mimeType) {
	// Create upload stream

	FileOutputStream fos = null; // Stream to write to
	try {
	    final EditMerchantProfileView editMerchantProfileView = (EditMerchantProfileView) this.getBasedComponent();
	    Map<String, Object> result = new HashMap<String, Object>();
	    if (!"image/jpeg".equals(mimeType) && !"image/jpg".equals(mimeType) && !"image/png".equals(mimeType)
		    && !"image/gif".equals(mimeType)) {
		editMerchantProfileView.getUlMerchantLogo().interruptUpload();
		result.put(MessageBAO.MESSAGE_WARNING_TYPE, "Mime Type: " + mimeType + "is not allowed.");
		this.showHint(editMerchantProfileView);
	    } else {
		// Create uploads directory
		String webInf = ApplicationHelper.getWebInfPath();
		String uploadPath = webInf + "/tmp/uploads/";
		File uploads = new File(uploadPath);
		if (!uploads.exists() && !uploads.mkdirs()) {
		    result.put(MessageBAO.MESSAGE_WARNING_TYPE, "Could not create upload dir");
		    this.showHint(editMerchantProfileView);
		} else {
		    // Open the file for writing.
		    file = new File(uploadPath + filename);
		    fos = new FileOutputStream(file);
		}
	    }
	    notification(result);
	} catch (final java.io.FileNotFoundException e) {
	    return null;
	}
	return fos; // Return the output stream to write to
    }

    @Override
    public boolean validate(ClickEvent event) throws Exception {
	return true;
    }

    @Override
    public Map<String, Object> click(ClickEvent event) throws Exception {
	return null;
    }
}
